<?php
include_once("ConnectionManager.php");
function getQtyReceipt(){
    $query = "SELECT COUNT(REC_ID) AS QTY FROM MITI_RECEIPT";
    ConnectionManager::getInstance()->setQuery($query);
    $array = ConnectionManager::getInstance()->query();
    $result = mysql_fetch_array($array);
    return $result["QTY"];
}

function findAllReceipt(){
	$query = "SELECT * FROM MITI_RECEIPT ORDER BY REC_DATE";
	ConnectionManager::getInstance()->setQuery($query);
    return ConnectionManager::getInstance()->query();
}

function createReceipt($userId, $code, $date, $deliveryingAddress, $payingAddress, $deliveryingDate, $phoneNumber){
	$query = "INSERT INTO MITI_RECEIPT(USER_ID, REC_CODE, REC_DATE, REC_STATUS, REC_DELIVERYINGADDRESS, REC_PAYINGADDRESS, REC_DELIVERYINGDATE, REC_PHONENUMBER) VALUES
                                 ($userId, '$code', str_to_date('$date', '%d/%m/%Y'), 1, '$deliveryingAddress', '$payingAddress', str_to_date('$deliveryingDate', '%d/%m/%Y'), '$phoneNumber')";
	ConnectionManager::getInstance()->setQuery($query);
    ConnectionManager::getInstance()->query();
	return mysql_insert_id();
}

function createReceiptItem($proId, $recId, $price, $quantity, $note){
	$query = "INSERT INTO MITI_RECEIPT_ITEM VALUES ($proId, $recId, $price, $quantity, '$note')";
	ConnectionManager::getInstance()->setQuery($query);
    ConnectionManager::getInstance()->query();
}

function updateStatus($recId, $status){
	$query = "UPDATE MITI_RECEIPT SET REC_STATUS = $status WHERE REC_ID = $recId";
	ConnectionManager::getInstance()->setQuery($query);
    ConnectionManager::getInstance()->query();
	return mysql_affected_rows() > 0;
}

function updateActuallyDeliveryingDate($recId, $date){
	$query = null;
	if($date == "")
		$query = "UPDATE MITI_RECEIPT SET REC_ACTUALLYDELIVERYINGDATE = NULL WHERE REC_ID = $recId";
	else
		$query = "UPDATE MITI_RECEIPT SET REC_ACTUALLYDELIVERYINGDATE = str_to_date('$date', '%d/%m/%Y') WHERE REC_ID = $recId";
	ConnectionManager::getInstance()->setQuery($query);
    ConnectionManager::getInstance()->query();
	return mysql_affected_rows() > 0;
}

function updateReceipt($recId, $status, $note){
	$query = "UPDATE MITI_RECEIPT SET REC_STATUS = $status, REC_NOTE = '$note' WHERE REC_ID = $recId";
	ConnectionManager::getInstance()->setQuery($query);
    ConnectionManager::getInstance()->query();
	return mysql_affected_rows() > 0;
}

function findReceiptBy($recId){
	$query = "SELECT * FROM MITI_RECEIPT WHERE REC_ID = $recId";
	ConnectionManager::getInstance()->setQuery($query);
    return ConnectionManager::getInstance()->query();
}

function findReceiptItemBy($recId){
	$query = "SELECT * FROM MITI_RECEIPT_ITEM WHERE REC_ID = $recId";
	ConnectionManager::getInstance()->setQuery($query);
    return ConnectionManager::getInstance()->query();
}

function findAllStatus(){
	$statuses = array(1 => "Đang chờ xử lý", 
					  2 => "Chờ thanh toán",
					  3 => "Đã thanh toán",
					  4 => "Đã giao hàng",
					  5 => "Đã hoàn tất",
					  6 => "Đã hủy bỏ",
					  7 => "Khách hàng hủy bỏ");
	return $statuses;
}

/////////// Receipt product option item ///////////////////////
function createReceiptProductOptionItem($proId, $recId, $optionItemId){
    $query = "INSERT INTO MITI_RECEIPT_PRODUCT_OPTIONITEM VALUES ($proId, $recId, $optionItemId)";
	ConnectionManager::getInstance()->setQuery($query);
    ConnectionManager::getInstance()->query();
}

function findReceiptProductOptionItemBy($proId, $recId){
    $query = "SELECT CONCAT((SELECT OP.OPP_NAME FROM MITI_OPTIONAL_PRICE OP WHERE OP.OPP_ID = OI.OPP_ID), ': ', OI.OOI_NAME) AS OPTIONITEM 
              FROM MITI_OPTION_ITEM OI 
              WHERE OI.OOI_ID IN(SELECT OPTIONITEMID 
                                 FROM MITI_RECEIPT_PRODUCT_OPTIONITEM 
                                 WHERE PRO_ID IN ($proId) AND REC_ID IN ($recId))";                                 
    ConnectionManager::getInstance()->setQuery($query);
    return ConnectionManager::getInstance()->query();
}
?>